package com.itheima.controller;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.aliyun.tea.TeaException;
import com.aliyun.teaopenapi.models.Config;
import com.aliyun.teautil.models.RuntimeOptions;
import com.itheima.common.JsonResult;
import com.itheima.common.Sample;
import com.itheima.common.SnowFlake;
import com.itheima.pojo.client.LoginDto;
import com.itheima.pojo.client.LoginVo;
import com.itheima.pojo.client.SendMsgDto;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.util.DigestUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Random;
import java.util.concurrent.TimeUnit;

/**
 * @author ChenJunJie
 * @date 2022/6/18 17:23
 * 用户controller层
 */
@RestController
@RequestMapping(value = "/user", produces = "application/json;charset=utf-8")
public class UserController {

    @Autowired
    private SnowFlake snowFlake;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;
    @Autowired
    private UserService userService;


    //用户登录
    @PostMapping("/login")
    public JsonResult<LoginVo> login(@RequestBody LoginDto loginDto){

        LoginVo login = userService.login(loginDto);


        if (login!=null){
            return JsonResult.loginSuccess(login,"登录成功");
        }else {
            return JsonResult.error("输入的验证码有误,请重新获取验证码");
        }
    }

    //用户登出
    @PostMapping("/loginout")
    public JsonResult<String> loginOut(HttpServletRequest request){
        userService.loginOut(request);
        return JsonResult.success("登出成功");
    }

    //短信验证码发送
    @PostMapping("/sendMsg")
    public JsonResult<String> sendMsg(@RequestBody SendMsgDto sendMsgDto) throws Exception {

        boolean b = userService.sendMsg(sendMsgDto);

        if (b){
            return JsonResult.success("手机验证码短信发送成功");
        }else {
            return JsonResult.error("手机验证码短信发送失败");
        }
    }
}
